package com.mcmaster.android.menus;

import java.util.ArrayList;
import java.util.List;

import com.mcmaster.android.R;
import com.mcmaster.android.data.Building;
import com.mcmaster.android.data.BuildingDatabase;
import com.mcmaster.android.data.Tour;
import com.mcmaster.android.data.TourDatabase;
import com.mcmaster.android.menus.GuidedToursMenu.ItemSelectListener;

import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.ListView;
import android.widget.Toast;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
//import android.widget.Adapter;
import android.widget.AdapterView.OnItemSelectedListener;

public class TourCreationMenu extends Activity {
	// Members
	ArrayAdapter<String> 	building_pool_adapter_;
	ArrayAdapter<String> 	added_buildings_adapter_;
	Spinner 				building_spinner_;
	ListView 				list_added_buildings_;
	EditText				tour_name_field_;
	EditText				tour_description_field_;
    
    Tour 					tour_ = new Tour();
    List<Building> 			buildings_;
    List<String> 			building_names_ = new ArrayList<String>();
    List<String> 			building_qrs_ = new ArrayList<String>();
    List<String> 			tour_building_names = new ArrayList<String>(); //<! Buildings added in tour
    
    int 					selected_index_ = 0; //!< Currently selected building index

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tour_creation_layout);      
        
        buildings_ = BuildingDatabase.dumpBuildings(); // Fetch buildings
        
        // Extract required building info
        for (int i=0; i<buildings_.size(); ++i) {
        	building_names_.add(buildings_.get(i).getName());
        	building_qrs_.add(buildings_.get(i).getQR());
        }
        
        // Spinners
        building_pool_adapter_ = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, building_names_);
        
        building_spinner_ = (Spinner)findViewById(R.id.spinner1);           
        building_spinner_.setAdapter(building_pool_adapter_);
        building_spinner_.setOnItemSelectedListener(new ItemSelectListener());
        
    	added_buildings_adapter_ = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tour_building_names);
    	
        list_added_buildings_ = (ListView)findViewById(R.id.listView1);
        list_added_buildings_.setAdapter(added_buildings_adapter_);
        
        // Buttons
        Button btn_reset = (Button)findViewById(R.id.buttonRes);
        Button btn_submit = (Button)findViewById(R.id.buttonSubmit);
        Button btn_add = (Button)findViewById(R.id.buttonAdd);
        
        btn_reset.setOnClickListener(new ButtonHandlerReset());
        btn_submit.setOnClickListener(new ButtonHandlerSubmit());
        btn_add.setOnClickListener(new ButtonHandlerAdd());
        
        tour_name_field_ = (EditText)findViewById(R.id.editText1);
        tour_description_field_ = (EditText)findViewById(R.id.textDescription);
        
       
    }
    private class ButtonHandlerReset implements View.OnClickListener {
	    public void onClick(View v) {
	    	tour_building_names.clear();
	    	tour_.clear();
	    	added_buildings_adapter_.notifyDataSetChanged();
	    	
	    	// Reset building names
	    	building_names_.clear();
	    	building_qrs_.clear();
	        for (int i=0; i<buildings_.size(); ++i) {
	        	building_names_.add(buildings_.get(i).getName());
	        	building_qrs_.add(buildings_.get(i).getQR());
	        }
	    }
	}
    private class ButtonHandlerSubmit implements View.OnClickListener {
	    public void onClick(View v) {
	    	tour_.name = tour_name_field_.getText().toString();
	    	tour_.description = tour_description_field_.getText().toString();
	    	TourDatabase.addTour(tour_);
	    	Toast.makeText(TourCreationMenu.this, "Tour creation successful!", Toast.LENGTH_SHORT).show();
	    	finish();
	    }
	}
    private class ButtonHandlerAdd implements View.OnClickListener {
	    public void onClick(View v) {
	    	if (selected_index_ != -1) {
	    		tour_.pushBuilding(building_qrs_.get(selected_index_));			// Add building to tour
	    		tour_building_names.add(building_names_.get(selected_index_));	// Show this change in spinner
	    		building_names_.remove(selected_index_);						// Remove this building from selection list
	    		building_qrs_.remove(selected_index_);
	    		added_buildings_adapter_.notifyDataSetChanged();				// Update spinners
	    		building_pool_adapter_.notifyDataSetChanged();
	            selected_index_ = 0;
	    	}
	    	else {
	    		// DISPLAY NO SELECTED BUILDING MSG
	    	}
	    	
	    }
	}
	public class ItemSelectListener implements OnItemSelectedListener {
		
		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {		
			selected_index_ = pos;
		}
		
		public void onNothingSelected(AdapterView parent) {
		}
	}
    
}
